#include "helper.h"
#include "ui_helper.h"

Helper::Helper(QWidget *parent) :
    BaseRestEditor(parent),
    ui(new Ui::Helper)
{
    ui->setupUi(this);

    normalMode=true;
    entry_=NC_HELPER;
    baseUrl_=URL_NC_HELPER;
    categories=QStringList();

    listModel=new QStringListModel(this);
    ui->listView->setModel(listModel);

    tableModel=new TableModel(this);
    ui->tableView->setModel(tableModel);

    createMenu();
    updateListView();
}

Helper::~Helper()
{
    delete ui;
}

void Helper::generateMenu()
{
    menu->addAction(QStringLiteral("添加... (F1)"),this,&Helper::newItem);
    menu->addSeparator();
    menu->addAction(QStringLiteral("编辑... (F2)"),this,&Helper::editItem);
    menu->addSeparator();
    menu->addAction(QStringLiteral("更新列表 (F4)"),this,&Helper::updateListView);
    menu->addAction(QStringLiteral("更新视图 (F5)"),this,&Helper::updateTableView);
    menu->addSeparator();
    QMenu *optMenu=menu->addMenu("选项");
    optMenu->addAction(QStringLiteral("删除 (Delete)"),this,&Helper::removeItem);
}

void Helper::updateListView()
{
    QString url=baseUrl_ + "get_categories";
    emit get(url);
}

void Helper::updateTableView()
{
    if(selectCategory.isEmpty())
        return;

    normalMode=true;
    tableModel->clear();
    QString url = baseUrl_ + "by_category/" + selectCategory;
    emit get(url);
}

void Helper::updateSearchView()
{
    if(keyword.isEmpty())
        return;

    normalMode=false;
    tableModel->clear();
    QString url = baseUrl_ + "search/" + keyword;
    emit get(url);
}

void Helper::newItem()
{
    P_AddItem addItem(this,categories);
    if(!selectCategory.isEmpty())
        addItem.setCategory(selectCategory);

    connect(&addItem,&P_AddItem::addItem,
            this,&Helper::onAddItem);
    if(addItem.exec() == QDialog::Accepted)
        delayUpdateView();
}

void Helper::editItem()
{
    QModelIndex index=ui->tableView->currentIndex();
    if(!index.isValid())
        return;

    int id=sp_fetchId(index);
    QString category=sp_fetchString(index,1);
    QString label=sp_fetchString(index,2);
    QString detail=sp_fetchString(index,3);
    QString addition=sp_fetchString(index,4);

    P_AddItem addItem(this,categories);
    addItem.setItem(id,category,label,detail,addition);

    connect(&addItem,&P_AddItem::updateItem,this,&Helper::onUpdateItem);
    if(addItem.exec() == QDialog::Accepted)
        delayUpdateView();
}

void Helper::removeItem()
{
    QModelIndex index=ui->tableView->currentIndex();
    if(!index.isValid())
        return;

    QMessageBox::StandardButton btn=MESSAGE_DELETE_CONFIRM
            if(btn == QMessageBox::Ok)
    {
            int id=sp_fetchId(index);
            if(id == 0)
    {
            MESSAGE_EXCEPTIOM
}
            else {
            QString url=baseUrl_ + "remove/" + QString::number(id);
            emit get(url);
            delayUpdateView();
}
}
}

void Helper::onAddItem(const QJsonObject &obj)
{
    QString url=baseUrl_ + "insert";
    emit post(url,QJsonDocument(obj).toJson(QJsonDocument::Compact));
}

void Helper::onUpdateItem(const QJsonObject &obj)
{
    QString url=baseUrl_ + "update";
    emit post(url,QJsonDocument(obj).toJson(QJsonDocument::Compact));
}

void Helper::delayUpdateView()
{
    delay();
    if(normalMode)
        updateTableView();
    else
        updateSearchView();
}

void Helper::onReceivedObject(const QJsonObject &object)
{
    if(!object.contains("entry") || object.value("entry").toString() != entry_)
        return;

    if(object.contains("detail"))
    {
        QJsonArray array=object.value("detail").toArray();
        if(array.isEmpty())
            return;

        tableModel->setArray(array,helperColumns,helperColumnHeaders);
        ui->tableView->hideColumn(0);   // ID
        ui->tableView->setColumnWidth(2,180); //label
        ui->tableView->hideColumn(4);   // addition
    }
    else if(object.contains("categories"))
    {
        QJsonArray array=object.value("categories").toArray();
        Piv piv(array);
        categories=piv.getStringList();
        listModel->setStringList(categories);
    }
}

void Helper::on_kw_returnPressed()
{
    keyword=ui->kw->text().trimmed();
    updateSearchView();
}

void Helper::on_listView_doubleClicked(const QModelIndex &index)
{
    if(index.isValid())
    {
        selectCategory=index.data().toString();
        updateTableView();
    }
}

void Helper::on_tableView_clicked(const QModelIndex &index)
{
    if(index.isValid())
    {
        QString addition=sp_fetchString(index,4);
        if(addition.isEmpty())
            return;

        ui->addition->setPlainText(addition);
    }
}
